home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / php / pear / Var_Dump.php < prev   
PHP Script  |  2004-10-01  |  18KB  |  465 lines

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2004 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 3.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available through the world-wide-web at the following url:           |
  11. // | http://www.php.net/license/3_0.txt.                                  |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Frederic Poeydomenge <frederic.poeydomenge@free.fr>         |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id:
  20.  
  21. require_once 'PEAR.php';
  22. require_once 'Var_Dump/Renderer.php';
  23.  
  24. /**
  25.  * Wrapper for the var_dump function.
  26.  *
  27.  * " The var_dump function displays structured information about expressions
  28.  * that includes its type and value. Arrays are explored recursively
  29.  * with values indented to show structure. "
  30.  *
  31.  * The Var_Dump class captures the output of the var_dump function,
  32.  * by using output control functions, and then uses external renderer
  33.  * classes for displaying the result in various graphical ways :
  34.  * simple text, HTML/XHTML text, HTML/XHTML table, XML, ...
  35.  *
  36.  * @package Var_Dump
  37.  * @category PHP
  38.  * @author Frederic Poeydomenge <frederic.poeydomenge@free.fr>
  39.  */
  40.  
  41. define ('VAR_DUMP_START_GROUP',           1);
  42. define ('VAR_DUMP_FINISH_GROUP',          2);
  43. define ('VAR_DUMP_START_ELEMENT_NUM',     3);
  44. define ('VAR_DUMP_START_ELEMENT_STR',     4);
  45. define ('VAR_DUMP_FINISH_ELEMENT',        5);
  46. define ('VAR_DUMP_FINISH_STRING',         6);
  47.  
  48. define ('VAR_DUMP_TYPE_ARRAY',            1);
  49. define ('VAR_DUMP_TYPE_OBJECT',           2);
  50.  
  51. define ('VAR_DUMP_PREG_MATCH',            0);
  52. define ('VAR_DUMP_PREG_SPACES',           1);
  53. define ('VAR_DUMP_PREG_KEY_QUOTE',        2);
  54. define ('VAR_DUMP_PREG_KEY',              3);
  55. define ('VAR_DUMP_PREG_STRING_TYPE',      4);
  56. define ('VAR_DUMP_PREG_STRING_VALUE',     5);
  57. define ('VAR_DUMP_PREG_VALUE',            6);
  58. define ('VAR_DUMP_PREG_VALUE_REFERENCE',  7);
  59. define ('VAR_DUMP_PREG_VALUE_TYPE',       8);
  60. define ('VAR_DUMP_PREG_VALUE_COMPL',      9);
  61. define ('VAR_DUMP_PREG_VALUE_RESOURCE',  10);
  62. define ('VAR_DUMP_PREG_ARRAY_END',       11);
  63. define ('VAR_DUMP_PREG_ARRAY_START',     12);
  64. define ('VAR_DUMP_PREG_ARRAY_TYPE',      13);
  65. define ('VAR_DUMP_PREG_ARRAY_COUNT',     14);
  66. define ('VAR_DUMP_PREG_STRING_COMPL',    15);
  67.  
  68. class Var_Dump
  69. {
  70.  
  71.     /**
  72.      * Run-time configuration options.
  73.      * @var array
  74.      * @access public
  75.      */
  76.     var $options = array();
  77.  
  78.     /**
  79.      * Default configuration options.
  80.      * @var array
  81.      * @access public
  82.      */
  83.     var $defaultOptions = array(
  84.         'display_mode' => 'XHTML_Text'
  85.     );
  86.  
  87.     /**
  88.      * Rendering configuration options.
  89.      * See Var_Dump/Renderer/*.php for the complete list of options
  90.      * @var array
  91.      * @access public
  92.      */
  93.     var $rendererOptions = array();
  94.  
  95.     /**
  96.      * Rendering object.
  97.      * @var object
  98.      * @access public
  99.      */
  100.     var $renderer = NULL;
  101.  
  102.     /**
  103.      * Class constructor.
  104.      * The factory approach must be used in relationship with the
  105.      * toString() method.
  106.      * See Var_Dump/Renderer/*.php for the complete list of options
  107.      * @see Var_Dump::toString()
  108.      * @param array $options         Global parameters.
  109.      * @param array $rendererOptions Parameters for the rendering.
  110.      * @access public
  111.      */
  112.     function Var_Dump($options = array(), $rendererOptions = array())
  113.     {
  114.         $this->options = array_merge (
  115.             $this->defaultOptions,
  116.             $options
  117.         );
  118.         $this->rendererOptions = $rendererOptions;
  119.         $this->renderer = & Var_Dump_Renderer::factory(
  120.             $this->options['display_mode'],
  121.             $this->rendererOptions
  122.         );
  123.     }
  124.  
  125.     /**
  126.      * Attempt to return a concrete Var_Dump instance.
  127.      * The factory approach must be used in relationship with the
  128.      * toString() method.
  129.      * See Var_Dump/Renderer/*.php for the complete list of options
  130.      * @see Var_Dump::toString()
  131.      * @param array $options         Global parameters.
  132.      * @param array $rendererOptions Parameters for the rendering.
  133.      * @access public
  134.      */
  135.     function & factory($options = array(), $rendererOptions = array())
  136.     {
  137.         $obj = & new Var_Dump($options, $rendererOptions);
  138.         return $obj;
  139.     }
  140.  
  141.     /**
  142.      * Uses a renderer object to return the string representation of a variable.
  143.      * @param mixed $expression The variable to parse.
  144.      * @return string The string representation of the variable.
  145.      * @access public
  146.      */
  147.     function toString($expression)
  148.     {
  149.  
  150.         if (PEAR::isError($this->renderer)) {
  151.             return '';
  152.         }
  153.  
  154.         $family = array(); // element family
  155.         $depth  = array(); // element depth
  156.         $type   = array(); // element type
  157.         $value  = array(); // element value
  158.  
  159.         // Captures the output of the var_dump function,
  160.         // by using output control functions.
  161.  
  162.         ob_start();
  163.         var_dump($expression);
  164.         $variable = ob_get_contents();
  165.         ob_end_clean();
  166.  
  167.         // Regexp that parses the output of the var_dump function.
  168.         // The numbers between square brackets [] are the reference
  169.         // of the captured subpattern, and correspond to the entries
  170.         // in the resulting $matches array.
  171.  
  172.         preg_match_all(
  173.             '!^
  174.               (\s*)                                 # 2 spaces for each depth level
  175.               (?:                                   #
  176.                 (?:\[("?)(.*?)\\2\]=>)              # Key [2-3]
  177.                   |                                 #   or
  178.                 (?:(&?string\(\d+\))\s+"(.*))       # String [4-5]
  179.                   |                                 #   or
  180.                 (                                   # Value [6-10]
  181.                   (&?)                              #   - reference [7]
  182.                   (bool|int|float|resource|         #   - type [8]
  183.                   NULL|\*RECURSION\*|UNKNOWN:0)     #
  184.                   (?:\((.*?)\))?                    #   - complement [9]
  185.                   (?:\sof\stype\s\((.*?)\))?        #   - resource [10]
  186.                 )                                   #
  187.                   |                                 #   or
  188.                 (})                                 # End of array/object [11]
  189.                   |                                 #   or
  190.                 (?:(&?(array|object)\((.+)\).*)\ {) # Start of array/object [12-14]
  191.                   |                                 #   or
  192.                 (.*)                                # String (additional lines) [15]
  193.               )                                     #
  194.             $!Smx',
  195.             $variable,
  196.             $matches,
  197.             PREG_SET_ORDER
  198.         );
  199.  
  200.         // Frees the memory used by the temporary variable
  201.         unset($variable);
  202.  
  203.         // Used to keep the maxLen of the keys for each nested variable.
  204.  
  205.         $stackLen = array();
  206.         $keyLen = array();
  207.         $maxLen = 0;
  208.  
  209.         // Used when matching a string, to count the remaining
  210.         // number of chars before the end of the string.
  211.  
  212.         $countdown = 0;
  213.  
  214.         // Used to keep a pointer (reference) on every
  215.         // variable parsed through the following loop.
  216.  
  217.         $reference = & $expression;
  218.         $stackReference = array();
  219.         $arrReference = array(
  220.             VAR_DUMP_PREG_STRING_TYPE,
  221.             VAR_DUMP_PREG_STRING_VALUE,
  222.             VAR_DUMP_PREG_ARRAY_START,
  223.             VAR_DUMP_PREG_ARRAY_TYPE,
  224.             VAR_DUMP_PREG_ARRAY_COUNT
  225.         );
  226.  
  227.         // Loop through the matches of the previously defined regexp.
  228.  
  229.         foreach($matches AS $match) {
  230.  
  231.             $count = count($match) - 1;
  232.  
  233.             // If we are waiting for additional lines of a string, decrease
  234.             // the countdown by the len of the match + 1 (\n),
  235.             // and skip to next loop.
  236.  
  237.             if ($countdown > 0) {
  238.                 $countdown -= strlen($match[VAR_DUMP_PREG_MATCH]) + 1;
  239.                 continue;
  240.             }
  241.  
  242.             // If matched a string or the beginning of an array/object,
  243.             // initialize a reference on the variable (=$obj).
  244.  
  245.             if (in_array($count, $arrReference)) {
  246.                 if (empty($stackReference)) {
  247.                     $obj = $reference;
  248.                 } else {
  249.                     list($vtyp, $vkey) = end($stackReference);
  250.                     switch ($vtyp) {
  251.                         case VAR_DUMP_TYPE_ARRAY:
  252.                             $obj = & $vkey[$reference];
  253.                             break;
  254.                         case VAR_DUMP_TYPE_OBJECT:
  255.                             $obj = & $vkey->$reference;
  256.                             break;
  257.                     }
  258.                 }
  259.             }
  260.  
  261.             // Find which alternative has been matched in the regexp,
  262.             // by looking at the number of elements in the $match array.
  263.  
  264.             switch ($count) {
  265.  
  266.                 // Key
  267.                 //=====
  268.                 // - Compute the maxLen of the keys at the actual depth
  269.                 // - Store the key name in $reference
  270.  
  271.                 case VAR_DUMP_PREG_KEY:
  272.                     $len = strlen($match[VAR_DUMP_PREG_KEY]);
  273.                     if ($len > $maxLen) {
  274.                         $maxLen = $len;
  275.                     }
  276.                     if (empty($match[VAR_DUMP_PREG_KEY_QUOTE])) {
  277.                         $family[] = VAR_DUMP_START_ELEMENT_NUM;
  278.                         $reference = (integer) $match[VAR_DUMP_PREG_KEY];
  279.                     } else {
  280.                         $family[] = VAR_DUMP_START_ELEMENT_STR;
  281.                         $reference = (string) $match[VAR_DUMP_PREG_KEY];
  282.                     }
  283.                     $depth[] = strlen($match[VAR_DUMP_PREG_SPACES]) >> 1;
  284.                     $type[]  = NULL;
  285.                     $value[] = $match[VAR_DUMP_PREG_KEY];
  286.                     break;
  287.  
  288.                 // String
  289.                 //========
  290.                 // - Set the countdown (remaining number of chars before eol) =
  291.                 //   len of the string - matched len + 1 (final ")
  292.  
  293.                 case VAR_DUMP_PREG_STRING_TYPE:
  294.                 case VAR_DUMP_PREG_STRING_VALUE:
  295.                     $countdown =
  296.                         strlen($obj)
  297.                         - strlen($match[VAR_DUMP_PREG_STRING_VALUE])
  298.                         + 1;
  299.                     $family[] = VAR_DUMP_FINISH_STRING;
  300.                     $depth[] = strlen($match[VAR_DUMP_PREG_SPACES]) >> 1;
  301.                     $type[] = $match[VAR_DUMP_PREG_STRING_TYPE];
  302.                     $value[] = $obj;
  303.                     break;
  304.  
  305.                 // Value
  306.                 //=======
  307.  
  308.                 case VAR_DUMP_PREG_VALUE:
  309.                 case VAR_DUMP_PREG_VALUE_REFERENCE:
  310.                 case VAR_DUMP_PREG_VALUE_TYPE:
  311.                 case VAR_DUMP_PREG_VALUE_COMPL:
  312.                 case VAR_DUMP_PREG_VALUE_RESOURCE:
  313.                     $family[] = VAR_DUMP_FINISH_ELEMENT;
  314.                     $depth[] = strlen($match[VAR_DUMP_PREG_SPACES]) >> 1;
  315.                     switch ($match[VAR_DUMP_PREG_VALUE_TYPE]) {
  316.                         case 'bool':
  317.                         case 'int':
  318.                         case 'float':
  319.                             $type[] =
  320.                                 $match[VAR_DUMP_PREG_VALUE_REFERENCE] .
  321.                                 $match[VAR_DUMP_PREG_VALUE_TYPE];
  322.                             $value[] = $match[VAR_DUMP_PREG_VALUE_COMPL];
  323.                             break;
  324.                         case 'resource':
  325.                             $type[] =
  326.                                 $match[VAR_DUMP_PREG_VALUE_REFERENCE] .
  327.                                 $match[VAR_DUMP_PREG_VALUE_TYPE] .
  328.                                 '(' . $match[VAR_DUMP_PREG_VALUE_RESOURCE] . ')';
  329.                             $value[] = $match[VAR_DUMP_PREG_VALUE_COMPL];
  330.                             break;
  331.                         default:
  332.                             $type[] =
  333.                                 $match[VAR_DUMP_PREG_VALUE_REFERENCE] .
  334.                                 $match[VAR_DUMP_PREG_VALUE_TYPE];
  335.                             $value[] = NULL;
  336.                             break;
  337.                     }
  338.                     break;
  339.  
  340.                 // End of array
  341.                 //==============
  342.                 // - Pop the maxLen of the keys off the end of the stack
  343.                 // - Pop the reference on the variable off the end of the stack
  344.                 // - If the last element on the stack is an array(0) or object(0),
  345.                 //   replace it by a standard element
  346.  
  347.                 case VAR_DUMP_PREG_ARRAY_END:
  348.                     $oldLen = array_pop($stackLen);
  349.                     $keyLen[$oldLen[0]] = $maxLen;
  350.                     $maxLen = $oldLen[1];
  351.                     list($vtyp, $vkey) = array_pop($stackReference);
  352.                     if (
  353.                         ($family[count($family) - 1] == VAR_DUMP_START_GROUP)
  354.                             and
  355.                         ($type[count($type) - 1] === 0)
  356.                     ) {
  357.                         $family[count($family) - 1] = VAR_DUMP_FINISH_ELEMENT;
  358.                         $type[count($type) - 1] = $value[count($value) - 1];
  359.                         $value[count($value) - 1] = NULL;
  360.                     } else {
  361.                         $family[] = VAR_DUMP_FINISH_GROUP;
  362.                         $depth[] = strlen($match[VAR_DUMP_PREG_SPACES]) >> 1;
  363.                         $type[] = NULL;
  364.                         $value[] = $match[VAR_DUMP_PREG_ARRAY_END];
  365.                                         }
  366.                     break;
  367.  
  368.                 // Start of array
  369.                 //================
  370.                 // - Push the maxLen of the keys onto the end of the stack
  371.                 // - Initialize new maxLen to 0
  372.                 // - Push the reference on the variable onto the end of the stack
  373.  
  374.                 case VAR_DUMP_PREG_ARRAY_START:
  375.                 case VAR_DUMP_PREG_ARRAY_TYPE:
  376.                 case VAR_DUMP_PREG_ARRAY_COUNT:
  377.                     array_push($stackLen, array(count($family), $maxLen));
  378.                     $maxLen = 0;
  379.                     switch ($match[VAR_DUMP_PREG_ARRAY_TYPE]) {
  380.                         case 'array':
  381.                             array_push(
  382.                                 $stackReference,
  383.                                 array(VAR_DUMP_TYPE_ARRAY, $obj)
  384.                             );
  385.                             break;
  386.                         case 'object':
  387.                             array_push(
  388.                                 $stackReference,
  389.                                 array(VAR_DUMP_TYPE_OBJECT, $obj)
  390.                             );
  391.                             break;
  392.                     }
  393.                     $family[] = VAR_DUMP_START_GROUP;
  394.                     $depth[] = strlen($match[VAR_DUMP_PREG_SPACES]) >> 1;
  395.                     $type[] = (int) $match[VAR_DUMP_PREG_ARRAY_COUNT];
  396.                     $value[] = $match[VAR_DUMP_PREG_ARRAY_START];
  397.                     break;
  398.  
  399.             } // switch ($count)
  400.  
  401.         }
  402.  
  403.         $this->renderer->initialize($family, $depth, $type, $value, $keyLen);
  404.  
  405.         $toString = $this->renderer->toString();
  406.  
  407.         // Frees the memory used by the matches
  408.         unset($matches);
  409.  
  410.         return $toString;
  411.  
  412.     }
  413.  
  414.     /**
  415.      * Attempt to return a concrete singleton Var_Dump instance.
  416.      * The singleton approach must be used in relationship with the
  417.      * displayInit() and display() methods.
  418.      * See Var_Dump/Renderer/*.php for the complete list of options
  419.      * @see Var_Dump::display(), Var_Dump::displayInit()
  420.      * @return object Var_Dump instance
  421.      * @access public
  422.      */
  423.     function & singleton()
  424.     {
  425.         static $instance;
  426.         if (!isset($instance)) {
  427.             $instance = new Var_Dump(array(), array());
  428.         }
  429.         return $instance;
  430.     }
  431.  
  432.     /**
  433.      * Initialise the singleton object used by the display() method.
  434.      * @see Var_Dump::singleton(), Var_Dump::display()
  435.      * @param array $options         Global parameters.
  436.      * @param array $rendererOptions Parameters for the rendering.
  437.      * @access public
  438.      */
  439.     function displayInit($options = array(), $rendererOptions = array())
  440.     {
  441.         $displayInit = & Var_Dump::singleton();
  442.         $displayInit->Var_Dump($options, $rendererOptions);
  443.     }
  444.  
  445.     /**
  446.      * Outputs or returns a string representation of a variable.
  447.      * @see Var_Dump::singleton(), Var_Dump::displayInit()
  448.      * @param mixed $expression The variable to parse.
  449.      * @param bool  $return     Whether the variable should be echoed or returned.
  450.      * @return string If returned, the string representation of the variable.
  451.      * @access public
  452.      */
  453.     function display($expression, $return=FALSE)
  454.     {
  455.         $display = & Var_Dump::singleton();
  456.         if ($return) {
  457.             return $display->toString($expression);
  458.         } else {
  459.             echo $display->toString($expression);
  460.         }
  461.     }
  462.  
  463. }
  464.  
  465. ?>